<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录</title>
</head>
<body>
<div id="app">
    <el-form label-width="80px">
        <el-form-item label="账号">
            <el-input v-model="user.userName"></el-input>
        </el-form-item>
        <el-form-item label="密码">
            <el-input v-model="user.password"></el-input>
        </el-form-item>

        <el-form-item label="验证码">
            <el-input v-model="user.check"></el-input>
        </el-form-item>
        <div class="demo-image__placeholder">
            <div class="block">
                <span class="demonstration">验证码</span>
                <el-image :src="YZM" @click="checkCode"></el-image>
            </div>
        </div>

        <el-form-item>
            <el-button type="primary" @click="login">立即登录</el-button>
        </el-form-item>
        <el-link href="http://localhost:8080/javaweb/static/html/register.html" target="_blank">去注册</el-link>
    </el-form>
</div>

</body>

<script src="../js/vue.js"></script>
<script src="../element-ui/lib/index.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<link rel="stylesheet" href="../element-ui/lib/theme-chalk/index.css">

<script>
    new Vue({
        el: "#app",
        data: {
            user: {
                userName: "",
                password: "",
                check: ""
            },
            YZM: ""
        },
        mounted() {
            this.checkCode()
        },
        methods: {
            checkCode() {
                var _this = this;
                var date = Date.parse(new Date());
                axios({
                    method: "get",
                    url: "http://localhost:8080/javaweb/check/checkCodeServlet"
                }).then(function (resp) {
                    _this.YZM = "http://localhost:8080/javaweb/check/checkCodeServlet?" + date
                })
            },
            login() {
                var _this = this;
                axios({
                    method: "post",
                    url: "http://localhost:8080/javaweb/app/loginServlet",
                    data: _this.user
                }).then(function (response) {
                    if (!response.data.flag) {
                        alert(response.data.msg);
                        _this.checkCode();
                    } else {
                        alert(response.data.msg);
                        _this.checkCode();
                        self.location.href = "http://localhost:8080/javaweb/static/html/posts.html";
                    }
                })
            }
        }
    })
</script>
</html>